package com.baturu.vin.hyundai.dealV2Mapping;

import com.baturu.vin.hyundai.dal.dao.DealPartsDAO;
import com.baturu.vin.hyundai.dto.MappingV2;
import com.baturu.vin.test.AbstractBaseCase;
import com.google.common.base.Splitter;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chenjiahao
 * @Time: 2019/8/24
 */
public class V2Mapping extends AbstractBaseCase {

    @Autowired
    DealPartsDAO dealPartsDAO;

    @Test
    public void dealPartsId() {
        int hyundaiPartsMaxId = dealPartsDAO.findHyundaiPartsMaxId();
        int start = 0;
        int stamp = 100000;
        int end = 0;
        while (start<hyundaiPartsMaxId){
            end = start+stamp;
            dealPartsDAO.updatePartsId(start, end);
            System.out.println("已更新到"+end);
            start+=stamp;
        }
        if(end<hyundaiPartsMaxId){
            dealPartsDAO.updatePartsId(end, hyundaiPartsMaxId);
        }
    }

    @Test
    public void insert() {
        int hyundaiPartsMaxId = dealPartsDAO.findHyundaiPartsMaxId();
//        int hyundaiPartsMaxId = dealPartsDAO.findHyundaiDcPartsMaxId();
        int start = 8180000;
        int stamp = 20000;
        int end = 0;
        String sql ="insert ignore into hyundaikia_parts_mapping_v2_extend (parts_id,btr_parts_name,dir_list,image_code,position)\n" +
//                "select  tt.parts_id,t.btr_parts_name,tt.bpno,tt.groupcode,tt.finalgroupcode from hyundai_mapping_info t\n" +//602000
//                "INNER JOIN hyundai_parts_ljd tt on left(t.finalPartsCode,5) = left(tt.finalpartcode,5) \n" +
//                "and t.finalPartsName= tt.finalPartName \n" +
//                "and t.groupName = tt.groupname \n" +
//                "and t.groupcode = tt.groupcode\n" +
//                "and t.groupNumber = tt.finalgroupcode ";
        "select  tt.parts_id,t.btr_parts_name,tt.bpno,tt.groupcode,tt.finalgroupcode from hyundai_parts_ljd tt\n" +
                "INNER JOIN  hyundai_mapping_info t on t.pre_partscode = tt.pre_partscode \n" +
                "and t.finalPartsName= tt.finalPartName \n" +
                "and t.groupName = tt.groupname \n" +
                "and t.groupcode = tt.groupcode\n" +
                "and t.groupNumber = tt.finalgroupcode ";
//        String sql = "insert into hyundai_mapping_info\n" +
//                "select DISTINCT finalPartsCode,finalPartsName,groupName,groupCode,groupNumber,btr_parts_name from hyundaikia_parts_mapping_v2 t\n" +
//                "INNER JOIN hyundai_parts tt on tt.parts_id = t.parts_id  ";
        while (start<hyundaiPartsMaxId){
            end = start+stamp;
            String sql1 = sql+"WHERE tt.id >= "+start+" AND tt.id<"+end;
            dealPartsDAO.insertParts(sql1);
//            dealPartsDAO.updateDcParts(start, end);
            System.out.println("已更新到"+end);
            start+=stamp;
        }
        if(end<hyundaiPartsMaxId){
//            dealPartsDAO.updateDcParts(end, hyundaiPartsMaxId);
            String sql1 = sql+"WHERE tt.id >= "+end+" AND tt.id<"+hyundaiPartsMaxId;
            dealPartsDAO.insertParts(sql1);
        }
    }

    @Test
    public void spliteMapping() {
        int hyundaiPartsMaxId = dealPartsDAO.findHyundaiDcPartsMaxId();
        int start = 0;
        int stamp = 100000;
        int end = 0;
        while (start<hyundaiPartsMaxId){
            end = start+stamp;
            dealMapping(start, end);
            System.out.println("已更新到"+end);
            start+=stamp;
        }

        if(end<hyundaiPartsMaxId){
            dealMapping(end, hyundaiPartsMaxId);
        }
    }

    private void dealMapping(int start, int end) {
        List<MappingV2> parts = dealPartsDAO.queryDcParts(start,end);
        if (CollectionUtils.isEmpty(parts)) {
            return;
        }
        parts = parts.stream().flatMap(d -> {
            List<String> partsName = Splitter.on(";").splitToList(d.getBtrPartsName());
            return partsName.stream().map(q -> MappingV2.builder().btrPartsName(q).dirList(d.getDirList()).partsId(d.getPartsId()).build());
        }).collect(Collectors.toList());
        dealPartsDAO.insertMapping(parts);
        System.out.println("已插入"+parts.size());
    }


}
